package com.tinywind.admin.module.system.utils;

import com.tinywind.admin.module.system.vo.MenuMetaVo;
import com.tinywind.admin.module.system.vo.MenuVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuxingyu01
 * @date 2021-12-26-20:38
 **/
public class MenuTreeUtil {

    /**
     * 获取菜单树(list转tree)
     *
     * @param treeList
     * @param pid
     * @return
     */
    public static List<MenuVo> toTree(List<MenuVo> treeList, String pid) {
        List<MenuVo> retList = new ArrayList<>();
        for (MenuVo parent : treeList) {

            // 父级是0，类型是目录，并且没有子级的情况，需要单独处理
            if (pid.equals(parent.getParentId()) && "1".equals(parent.getType())
                    && !hasChildren(treeList, parent.getPermissionId())) {
                // 如果是外链，则单独处理
                if ("1".equals(parent.getIframe())) {
                    MenuMetaVo meta = new MenuMetaVo();
                    meta.setIcon(parent.getIcon());
                    meta.setTitle(parent.getTitle());
                    meta.setHref(parent.getHref());
                    parent.setMeta(meta);
                    // parent.setPath("");
                } else {
                    MenuVo menuVo1 = new MenuVo();
                    menuVo1.setPath(parent.getPath());
                    menuVo1.setName(parent.getName());
                    menuVo1.setComponent(parent.getComponent());

                    MenuMetaVo meta = new MenuMetaVo();
                    meta.setIcon(parent.getIcon());
                    meta.setTitle(parent.getTitle());
                    meta.setNotCache(parent.getCache());
                    meta.setHideInBread(parent.getHideInBread());
                    meta.setHideInMenu(parent.getHideInMenu());

                    menuVo1.setMeta(meta);

                    List<MenuVo> list1 = new ArrayList<>();
                    list1.add(menuVo1);

                    // 重新组织父级
                    MenuMetaVo meta2 = new MenuMetaVo();
                    meta2.setHideInBread(true);

                    parent.setName("par_" + parent.getName());
                    parent.setPath("/par_" + parent.getPath());

                    parent.setMeta(meta2);
                    parent.setComponent("Main");
                    parent.setChildren(list1);
                }
                retList.add(parent);
            } else {
                MenuMetaVo menuMetaVo = new MenuMetaVo();
                // parentId为0，且为目录的情况下，说明为最高级目录，则compnent为Main
                if (pid.equals(parent.getParentId()) && "0".equals(parent.getType())) {
                    parent.setComponent("Main");
                    menuMetaVo.setShowAlways(true);
                }
                // parentId不为0，说明是子目录，则compnent为parentView
                if (!pid.equals(parent.getParentId()) && "0".equals(parent.getType())) {
                    parent.setComponent("parentView");
                    menuMetaVo.setShowAlways(true);
                }

                menuMetaVo.setIcon(parent.getIcon());
                menuMetaVo.setTitle(parent.getTitle());
                menuMetaVo.setNotCache(parent.getCache());
                menuMetaVo.setHideInMenu(parent.getHideInMenu());
                menuMetaVo.setHideInBread(parent.getHideInBread());

                parent.setMeta(menuMetaVo);

                if (pid.equals(parent.getParentId())) {
                    retList.add(findChildren(parent, treeList));
                }
            }
        }
        return retList;
    }


    /**
     * 获取子节点
     *
     * @param parent
     * @param treeList
     * @return
     */
    private static MenuVo findChildren(MenuVo parent, List<MenuVo> treeList) {
        for (MenuVo child : treeList) {
            if (parent.getPermissionId().equals(child.getParentId())) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(findChildren(child, treeList));
            }
        }
        return parent;
    }


    /**
     * 判断是否有子级
     * @param treeList
     * @param permissionId
     * @return true有子级  false无子级
     */
    private static boolean hasChildren(List<MenuVo> treeList, String permissionId) {
        boolean hasChildren = false;
        for (MenuVo menuVo: treeList) {
            if (menuVo.getParentId().equals(permissionId)) {
                hasChildren= true;
                break;
            }
        }
        return hasChildren;
    }


}
